#include "IPCClient.hpp"

static const char* Servername="toot.camera.dhclient";
static const char* ObjectPath="/toot/camera/dhclient";
static const char* Interface="toot.camera.dhclient";

using namespace std::chrono_literals;

void IPCClient::Boot()
{
  ClientThread=std::thread(&IPCClient::ClientWork,this);
}

IPCClient::~IPCClient()
{
  if(ClientThread.joinable())
  {
    conn->leaveEventLoop();
    ClientThread.join();
  }
}

void IPCClient::ClientWork()
{
  conn=sdbus::createSessionBusConnection();
  proxy=sdbus::createProxy(*conn,Servername,ObjectPath);
  
  proxy->uponSignal("Message").onInterface(Interface).call(funcMessage);
  proxy->uponSignal("CameraOffline").onInterface(Interface).call(funcCameraOffline);
  proxy->finishRegistration();

  conn->enterEventLoop();
}

void IPCClient::StartShot(const std::string& uuid)
{
  proxy->callMethod("StartShot").onInterface(Interface).withArguments(uuid).withTimeout(3s).dontExpectReply();
}

void IPCClient::StopShot(const std::string& uuid)
{
  proxy->callMethod("StopShot").onInterface(Interface).withArguments(uuid).withTimeout(3s).dontExpectReply();
}

std::vector<std::string> IPCClient::GetAllDevices()
{
  std::vector<std::string> res;
  proxy->callMethod("GetAllDevice").onInterface(Interface).withTimeout(3s).storeResultsTo(res);
  return res;
}

bool IPCClient::ShotStatus(const std::string& uuid)
{
  bool status;
  proxy->callMethod("ShotStatus").onInterface(Interface).withTimeout(3s).storeResultsTo(status);
  return status;
}
